﻿using System;
using System.Text;
using UnityEngine;

public class ExcelLineReader
{
    protected string m_FileName;    // 当前文件名称
    protected int m_LineIdx;        // 当前读取行数
    protected int m_ColumnIdx;      // 当前读取列数
    protected Parser m_Parser;      // 行解析器对象

    public ExcelLineReader(string fileName, int lineIdx, byte[] buffer, int len)
    {
        m_FileName = fileName;
        m_LineIdx = lineIdx;
        m_ColumnIdx = 0;
        m_Parser = new Parser(buffer, len, '\t');
    }

    // 跳过一列
    public void Skip()
    {
        m_ColumnIdx++;
        m_Parser.Skip();
    }

    // 读取byte
    public byte ReadByte()
    {
        m_ColumnIdx++;
        byte ret = 0;

        if (!m_Parser.ReadByte(ref ret))
        {
            ReadError();
        }
        return ret;
    }

    // 读取char
    public char ReadChar()
    {
        m_ColumnIdx++;
        char ret = '\0';

        if (!m_Parser.ReadChar(ref ret))
        {
            ReadError();
        }
        return ret;
    }

    // 读取short
    public short ReadShort()
    {
        m_ColumnIdx++;
        short ret = 0;

        if (!m_Parser.ReadShort(ref ret))
        {
            ReadError();
        }
        return ret;
    }

    // 读取int
    public int ReadInt()
    {
        m_ColumnIdx++;
        int ret = 0;

        if (!m_Parser.ReadInt(ref ret))
        {
            ReadError();
        }
        return ret;
    }

    // 读取float
    public float ReadFloat()
    {
        m_ColumnIdx++;
        float ret = 0.0f;

        if (!m_Parser.ReadFloat(ref ret))
        {
            ReadError();
        }
        return ret;
    }

    // 读取boolean
    public bool ReadBool()
    {
        m_ColumnIdx++;
        bool ret = false;

        if (!m_Parser.ReadBool(ref ret))
        {
            ReadError();
        }
        return ret;
    }

    // 读取long
    public long ReadLong()
    {
        m_ColumnIdx++;
        long ret = 0;

        if (!m_Parser.ReadLong(ref ret))
        {
            ReadError();
        }
        return ret;
    }

    // 读取Short string
    public string ReadShortString()
    {
        m_ColumnIdx++;
        string ret = "";

        m_Parser.ReadShortString(ref ret);
        return ret;
    }

    // 读取string
    public string ReadString()
    {
        m_ColumnIdx++;
        string ret = "";

        m_Parser.ReadString(ref ret);
        return ret;
    }

    // 读取出错信息
    public void ReadError()
    {
        string msg = "ExcelLineReader: file=[{0:s}], line=[{1:d}], column=[{2:d}], Failed\n";
        Debug.LogError(string.Format(msg, m_FileName, m_LineIdx, m_ColumnIdx));
    }

    // 销毁处理
    public void Clear()
    {
        m_FileName = null;
        m_LineIdx = 0;
        m_ColumnIdx = 0;
        m_Parser.Clear();
        m_Parser = null;
    }
}

public class ExcelReader
{
    protected string m_FileName;        // 文件名称
    protected int m_LineIdx;            // 当前读取行
    protected DPacket m_ReaderDepacket; // 文件解包器

    private byte m_byteR = Convert.ToByte('\r');
    private byte m_byteN = Convert.ToByte('\n');
    private byte m_byte0 = Convert.ToByte('\0');

    // 读取一行数据
    public ExcelLineReader ReadLine()
    {
        ExcelLineReader ret = null;
        if (m_ReaderDepacket.GetReadPos() >= m_ReaderDepacket.Size())
        {
            return ret;
        }

        int num = 0; bool reset = false;
        byte[] buffer = new byte[Parser.STRING_MAXLENGTH];
        do
        {
            {
                int pos = 0;
                do
                {
                    if (pos == Parser.STRING_MAXLENGTH)
                    {
                        reset = true;
                        break;
                    }

                    if (m_ReaderDepacket.GetReadPos() >= m_ReaderDepacket.Size())
                    {
                        break;
                    }

                    buffer[pos] = m_ReaderDepacket.ReadByte();
                    if (pos > 0 && buffer[pos] == m_byteN)
                    {
                        if (buffer[pos - 1] == m_byteR)
                        {
                            buffer[pos - 1] = m_byte0;
                        }

                        buffer[pos] = m_byte0;
                        num = pos; break;
                    }
                    pos++;
                }
                while (true);
            }

            if (reset || num <= 2)
            {
                break;
            }

            m_LineIdx++;
            ret = new ExcelLineReader(m_FileName, m_LineIdx, buffer, num);
        }
        while (false);
        buffer = null; return ret;
    }

    public bool LoadFile(string fileName)
    {
        // 加载文件
        TextAsset textAsset = Resources.Load<TextAsset>(fileName);
        return LoadBuffer(Encoding.UTF8.GetBytes(textAsset.text));
    }

    // 加载文件数据流
    public bool LoadBuffer(byte[] text)
    {
        // 加载文件
        if (text == null) { return false; }
        m_ReaderDepacket = new DPacket(text, text.Length);
        
        byte now = m_byte0; byte pre = m_byte0;
        do
        {
            if (m_ReaderDepacket.GetReadPos() >= m_ReaderDepacket.Size())
            {
                return false;
            }
            pre = now; now = m_ReaderDepacket.ReadByte();
            if (now == m_byteN && pre == m_byteR) { break; }
        }
        while (true);
        
        now = m_byte0; pre = m_byte0;
        do
        {
            if (m_ReaderDepacket.GetReadPos() >= m_ReaderDepacket.Size())
            {
                return false;
            }
            pre = now; now = m_ReaderDepacket.ReadByte();
            if (now == m_byteN && pre == m_byteR) { break; }
        }
        while (true);
        
        now = m_byte0; pre = m_byte0;
        do
        {
            if (m_ReaderDepacket.GetReadPos() >= m_ReaderDepacket.Size())
            {
                return false;
            }
            pre = now; now = m_ReaderDepacket.ReadByte();
            if (now == m_byteN && pre == m_byteR) { break; }
        }
        while (true); return true;
    }

    // 销毁处理
    public void Clear()
    {
        m_LineIdx = 0;
        m_FileName = null;
        m_ReaderDepacket.Clear();
        m_ReaderDepacket = null;
    }
}